package com.hzy.fivepalace.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.common.util.TableDataInfo;
import com.hzy.fivepalace.db.dao.ex.DtsCategoryMapperEx;
import com.hzy.fivepalace.db.domain.DtsCategory;
import com.hzy.fivepalace.db.vo.wx.WxCategoryVo;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 新分类服务
 */
@Service
public class AdminNewCategoryService extends BaseController {


    @Resource
    private DtsCategoryMapperEx categoryMapper;

    @Resource
    private Redisson redisson;


    /**
     * 父级类目列表-分页
     *
     * @param page
     * @param size
     * @return
     */
    public TableDataInfo getFatherCategoryList(Integer page, Integer size) {

        Page p = PageHelper.startPage(page, size);
        List<WxCategoryVo> list = categoryMapper.getFatherCategoryList();
        if (CollectionUtils.isEmpty(list)) {
            list = new ArrayList<>();
        }
        return TableDataInfo.getDataTable(list, p.getTotal());
    }

    /**
     * 删除类目
     *
     * @param categoryId
     * @return
     */
    public Object delCategory(Integer categoryId) {
        RLock lock = redisson.getLock("admin_delNewCategory_" + categoryId);
        if (lock.isLocked()) {
            return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
        }
        lock.lock(30, TimeUnit.SECONDS);
        DtsCategory category = categoryMapper.selectByPrimaryKey(categoryId);
        if (null == category || category.getDeleted() || category.getIsHome()) {
            lock.unlock();
            return ResponseUtil.fail("分类不存在");
        }
        //判断该分类下是否存在有效商品
        if (categoryMapper.isExistGoods(categoryId,2)) {
            lock.unlock();
            return ResponseUtil.fail("该类目下存在有效商品,无法删除");
        }

        category.setUpdateTime(LocalDateTime.now());
        category.setDeleted(true);
        categoryMapper.updateByPrimaryKeySelective(category);
        lock.unlock();
        return ResponseUtil.ok("删除成功", null);
    }


    /**
     * 新增或修改首页类目
     *
     * @param vo
     * @param sort
     * @return
     */
    public Object addCategoryOrUpd(WxCategoryVo vo, Integer sort) {

        Integer adminId = getAdminId();
        if (null == vo.getCategoryId()) {//新增
            RLock lock = redisson.getLock("admin_addNewCategory_" + adminId);
            if (lock.isLocked()) {
                return ResponseUtil.frequent();
            }
            lock.lock(30, TimeUnit.SECONDS);
            if (StringUtils.isBlank(vo.getCategorynNme())) {
                lock.unlock();
                return ResponseUtil.fail("类目名称不能为空");
            }
            if (vo.getCategorynNme().length() > 10) {
                lock.unlock();
                return ResponseUtil.fail("类目名称最多10个字");
            }
            if (StringUtils.isBlank(vo.getPicUrl())) {
                vo.setPicUrl(null);
            }
            if (null == sort || sort.intValue() < 0) {
                sort = 0;
            }

            DtsCategory record = new DtsCategory();
            record.setName(vo.getCategorynNme());
            record.setPicUrl(vo.getPicUrl());
            record.setPid(0);
            record.setDeleted(false);
            record.setLevel("L1");
            record.setAddTime(LocalDateTime.now());
            record.setDesc(vo.getDesc());
            record.setUpdateTime(LocalDateTime.now());
            record.setSortOrder(sort.byteValue());
            record.setIsHome(false);
            categoryMapper.insertSelective(record);
            lock.unlock();
            return ResponseUtil.ok("新增成功", null);
        } else {//修改
            RLock lock = redisson.getLock("admin_updNewCategory_" + vo.getCategoryId());
            if (lock.isLocked()) {
                return ResponseUtil.frequent("其他管理人员操作中,请稍后再试");
            }
            lock.lock(30, TimeUnit.SECONDS);
            DtsCategory category = categoryMapper.selectByPrimaryKey(vo.getCategoryId());
            if (null == category || category.getDeleted() || category.getIsHome()) {
                lock.unlock();
                return ResponseUtil.fail("类目不存在");
            }
            if (StringUtils.isBlank(vo.getCategorynNme())) {
                vo.setCategorynNme(null);
            }
            if (!StringUtils.isBlank(vo.getCategorynNme()) && vo.getCategorynNme().length() > 10) {
                lock.unlock();
                return ResponseUtil.fail("类目名称最多10个字");
            }
            if (StringUtils.isBlank(vo.getPicUrl())) {
                vo.setPicUrl(null);
            }
            if (StringUtils.isBlank(vo.getDesc())) {
                vo.setDesc(null);
            }
            DtsCategory record = new DtsCategory();
            record.setId(vo.getCategoryId());
            record.setName(vo.getCategorynNme());
            record.setPicUrl(vo.getPicUrl());
            record.setDesc(vo.getDesc());
            record.setUpdateTime(LocalDateTime.now());
            if (null != sort) {
                record.setSortOrder(sort.byteValue());
            }
            categoryMapper.updateByPrimaryKeySelective(record);
            lock.unlock();
            return ResponseUtil.ok("修改成功", null);
        }
    }
}
